package com.sicau.controller;


import com.sicau.dao.ManagerMapper;
import com.sicau.entity.dto.NoticePicture;
import com.sicau.entity.dto.TaskInfo;
import com.sicau.entity.pojo.po.Pic;
import com.sicau.service.ManagerService;
import com.sicau.service.NoticePictureService;
import com.sicau.utils.CsvUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.*;

@RestController

public class UploadTestController {

    @Autowired
    private NoticePictureService noticePictureService;

    @Autowired
    private ManagerMapper managerMapper;


//    @RequestMapping("/test")
//    public String pathTest(ModelMap model) {
//        return "index";
//    }


    /**
     * 图片文件上传
     */
    @RequestMapping(value = "/test..", method = RequestMethod.POST)
    public String upload(MultipartFile file) throws Exception {
        //保存数据库的路径
        String sqlPath = null;
        //定义文件保存的本地路径
        String localPath = "D:\\File\\image\\";
        //定义 文件名
        String filename = null;
        if (!file.isEmpty()) {
            //生成uuid作为文件名称
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
            //获得文件类型（可以判断如果不是图片，禁止上传）
            String contentType = file.getContentType();
            //获得文件后缀名
            String suffixName = contentType.substring(contentType.indexOf("/") + 1);

            System.err.println(suffixName);

            //得到 文件名
            filename = uuid + "." + suffixName;
            //文件保存路径
            file.transferTo(new File(localPath + filename));
        }
        //把图片的相对路径保存至数据库
        NoticePicture noticePicture = new NoticePicture();
        sqlPath = "/file/image/" + filename;
        System.out.println(sqlPath);
        noticePicture.setPicPath(sqlPath);
        noticePictureService.addPic(noticePicture);
        return null;
    }



    @RequestMapping("test")
    public void uploadExcelModel(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //开始上传excel
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        MultipartFile multipartFile = multipartRequest.getFile("file");
        String location = multipartRequest.getParameter("location");

        //System.err.println(emmm);

        String sourceName = multipartFile.getOriginalFilename(); // 原始文件名
        Long size = multipartFile.getSize();
        String fileType = sourceName.substring(sourceName.lastIndexOf("."));
        System.out.println("上传的文件名为:"+sourceName+"类型为:"+fileType+"大小："+size);

        //保存数据库的路径
        String sqlPath = null;
        //定义文件保存的本地路径
        String localPath = "D:\\File\\image\\";
        //定义 文件名
        String filename = null;

        if (!multipartFile.isEmpty()) {
            //生成uuid作为文件名称
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
//            //获得文件类型（可以判断如果不是图片，禁止上传）
////            String contentType = multipartFile.getContentType();
////            //获得文件后缀名
            String suffixName = sourceName.substring(sourceName.lastIndexOf(".")+1);

            System.err.println(suffixName);

            //得到 文件名
            filename = uuid + "." + suffixName;
            //文件保存路径
            multipartFile.transferTo(new File(localPath + filename));
        }

        //把图片的相对路径保存至数据库
        NoticePicture noticePicture = new NoticePicture();
        sqlPath = "/file/image/" + filename;
        System.out.println(sqlPath);
        noticePicture.setPicPath(sqlPath);
        noticePicture.setPicName(sourceName);
        noticePicture.setLocation(location);
        noticePictureService.addPic(noticePicture);


    }


    /**
     * 更新csv数据
     * @return
     */
//    @RequestMapping("/upload..")
//    public void updatePayFeeData(HttpServletRequest request,HttpServletResponse response) {
//        //创建一个通用的多部分解析器
//        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
//        //判断 request 是否有文件上传,即多部分请求
//        int pre = (int) System.currentTimeMillis();
//        if (multipartResolver.isMultipart(request)) {
//            //转换成多部分request
//            MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
//            //取得request中的所有文件名
//            Iterator<String> iter = multiRequest.getFileNames();
//            while (iter.hasNext()) {
//                MultipartFile file = multiRequest.getFile(iter.next());
//                List<TaskInfo> taskInfos = CsvUtil.csvMappingObj(file);
//
//                managerMapper.addTasks(taskInfos);
//
//            }
//        }
//
//    }

}

